package com.anrongtec.zcpt.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.content.IntentCompat;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.constant.Constant;
import com.anrongtec.zcpt.ui.activity.LoginActivity;
import com.anrongtec.zcpt.utils.DialogUtil;
import com.anrongtec.zcpt.utils.LogUtil;
import com.sdsmdg.tastytoast.TastyToast;

/**
 * Created by wangyingying on 16/9/7.
 */
public class BaseActivity extends IBaseActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 设置为竖屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.action.STAFF_CHANGED);
        registerReceiver(receiver, filter);
    }

    private void showMyDialog() {
        LogUtil.trace(Log.DEBUG, "baseactivity", this.getLocalClassName());
        DialogUtil.createAlertDialogSingel(this, "切换用户", "检测到新用户指纹, 请重新登录.", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
            }
        });
    }

    private void logout() {
        Intent intent = new Intent(this, LoginActivity.class);
        ComponentName cn = intent.getComponent();
        Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
        startActivity(mainIntent);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    /**
     * 接收用户更变广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            showMyDialog();
        }
    };

    /**
     * Toast的5种状态使用
     * @param text
     * @param type
     */
    protected void showToastCustom(String text, int type){
        switch (type){
            case ApiConfig.SUCCESS:
            TastyToast.makeText(this,text, TastyToast.LENGTH_SHORT, TastyToast.SUCCESS);
                break;
            case ApiConfig.ERROR:
            TastyToast.makeText(this,text, TastyToast.LENGTH_SHORT, TastyToast.ERROR);
                break;
            case ApiConfig.WARNING:
            TastyToast.makeText(this,text, TastyToast.LENGTH_SHORT, TastyToast.WARNING);
                break;
            case ApiConfig.INFO:
            TastyToast.makeText(this,text, TastyToast.LENGTH_SHORT, TastyToast.INFO);
                break;
            case ApiConfig.DEFAULT:
            TastyToast.makeText(this,text, TastyToast.LENGTH_SHORT, TastyToast.DEFAULT);
                break;
        }
    }


    /**
     * 标题栏选择的模式
     * @param activity
     * @param isShow
     * @param title
     * @param stitle
     */
    public void initTopBar(final Activity activity,boolean isShow,String title,String stitle){
        if (isShow) {
            LinearLayout ll_back = (LinearLayout)activity.findViewById(R.id.ll_back);
            ll_back.setVisibility(View.VISIBLE);
            ll_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });
        }else {
            LinearLayout ll_back = (LinearLayout)activity.findViewById(R.id.ll_back);
            ll_back.setVisibility(View.GONE);
        }
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_stitle= (TextView) findViewById(R.id.tv_stitle);
        tv_title.setText(title);
        tv_stitle.setText(stitle);
    }
}
